package cn.edu.cdtu.healthclient.activity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import cn.edu.cdtu.healthclient.service.HealthService;
import cn.edu.cdtu.healthclient.service.HealthService.HealthBinder;
import cn.edu.cdtu.healthclient.task.ActivityTaskQueue;
import cn.edu.scu.healthclient.activity.R;

public class WelcomeActivity extends Activity {

	/*
	 * 
	 */
	private ActivityTaskQueue taskQueue;
	public ActivityTaskQueue getTaskQueue(){
		return taskQueue;
	}
	/*
	 * 
	 */
	private HealthBinder binder;
	public HealthBinder getBinder(){
		return binder;
	}
	/*
	 * 
	 */
	private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			binder = null;
			taskQueue.shutdownTaskQueue();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (HealthBinder)service;
			if(binder.getAttribute(
					LoginActivity.LOGIN_USER_OBJECT_NAME_STRING) != null){
				Intent mainIntent=new Intent(WelcomeActivity.this,MainActivity.class);
				mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				finish();
    			startActivity(mainIntent);
			}else {
				if(!taskQueue.isRunning()){
					taskQueue.start();
				}
			}
		}
	};
	
	/*
	 * 
	 */
	private final Intent serviceIntent = new Intent(HealthService.ACTION);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//启动HealthService服务，该组件运行于APP后台，负责app与服务器的连接等任务。
		startService(serviceIntent);
		//实例化一个Activity任务队列，负责管理Activity组件。其核心原理为多线程。
    	taskQueue = new ActivityTaskQueue();
    	//设置应用为全屏模式。
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		
		LinearLayout mLinear = (LinearLayout) findViewById(R.id.Fragment01Linear);
		mLinear.setBackgroundResource(R.drawable.ic_splash_screen);
		//向队列中添加任务。
		taskQueue.addTask(new Thread(){
			public void run(){
				try{
					//任务线程休眠1.5秒，及欢迎界面的时间为1.5秒，然后任务启动其他Activity。
					Thread.sleep(1500);
				}
				catch(InterruptedException e){
					e.printStackTrace();
				}
				//UI主线程启动任务，欢迎界面结束后启动登录界面。
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Intent intent=new Intent(WelcomeActivity.this,LoginActivity.class);
						//杀死当前WelcomeActivity。
						finish();
						//启动登录界面.
						startActivity(intent);
					}
				});
			}
		});
	}
	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		bindService(serviceIntent, 
        		serviceConnection, Service.BIND_AUTO_CREATE);
		super.onStart();
	}

	/* (non-Javadoc)
	 * <p>Title: onStop</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unbindService(serviceConnection);
	}
}
